home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: Q:order of evaluation
- Date: Fri, 26 Jan 1996 12:35:40 GMT
- Organization: Netcom
- Message-ID: <3108c867.40236096@nntp.ix.netcom.com>
- References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <hamilton-1801962045570001@dialup-147.austin.io.com> <4dpcfo$293@clarknet.clark.net> <hamilton-2401960104020001@dialup-86.austin.io.com>
- NNTP-Posting-Host: ix-dc7-03.ix.netcom.com
- X-NETCOM-Date: Fri Jan 26 4:35:41 AM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- hamilton@shokwave.com (Jim Hamilton) wrote:
-
- > In article <4dpcfo$293@clarknet.clark.net>, gusty@clark.net (Harlan
- > Messinger) wrote:
- >
- > >Jim Hamilton (hamilton@shokwave.com) wrote:
- > >: In article <4dfhlu$a33$1@mhafn.production.compuserve.com>, Holger Maier
- > >: <100336.3326@CompuServe.COM> wrote:
- > >:
- > >: >Consider
- > >: >#include <iostream>
- > >: >int main() {
- > >: > int i=1;int j=i+(i+=1);
- > >: > cout<<i<<','<<j<<'\n';
- > >: > return 0;
- > >: >}
- > >: >on my compiler this produces 2,4
- > [deleted]
- >
- > >: The highest precedence in any expression is the insides of parentheses
- > >: (). Therefore (i+=1) is evaluated before i+().
- > >
- > >True, but that's not actually the problem here. The problem is whether
- > >(i+=1) gets evaluated before the i to the left of the plus sign.
- >
- > And I said, what's inside the parentheses (i+=1) *does* gets evaluated
- > before the i to the left of the plus sign.
-
- You said it, but it's wrong. The order of evaluation of operands is
- undefined. In i + (i += 1), there are two operands of +, i and (i +=
- 1). Either may be evaluated first.
-
- Actually, this is theoretical since the draft, following the C
- standard, is much less forgiving (5)
-
- Except where noted, the order of evaluation of operands of
- individual operators and subexpressions of individual
- expressions, and the order in which side effects take place,
- is unspecified. Between the previous and next sequence
- point a scalar object shall have its stored value modified at
- most once by the evaluation of an expression. Furthermore,
- the prior value shall be accessed only to determine the
- value to be stored. The requirements of this paragraph shall
- be met for each allowable ordering of the subexpressions of a
- full expression; otherwise the behavior is undefined.
-
- [Example:
- i = v[i++]; // the behavior is undefined
- i = 7,i++,i++; // `i' becomes 9
-
- i = ++i + 1; // the behavior is undefined
- i = i + 1; // the value of 'i' is incremented
-
- There is no sequence point in i + (i += 1) save at the end of the full
- expression, so i is both modified and it's value accessed other than
- to determine the value to be stored. This results in undefined
- behavior and there are no requirements on the compilers actions -- it
- does not have to compute a value at all.
-
-
- Michael M Rubenstein
-